package com.quanyan.travel.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.google.common.collect.Lists;
import com.google.gson.Gson;
import com.quanyan.api.APIResponse;
import com.quanyan.comment.constants.UserCenterConstants;
import com.quanyan.comment.reqeust.ReqUserCollectQuery;
import com.quanyan.comment.serviceFacade.UserCollectServiceFacade;
import com.quanyan.common.Plugin.Page;
import com.quanyan.common.Plugin.PageObj;
import com.quanyan.common.utils.DateUtils;
import com.quanyan.redisCluster.utils.RedisConstants;
import com.quanyan.redisCluster.utils.RedisKey;
import com.quanyan.travel.api.req.*;
import com.quanyan.travel.api.resp.*;
import com.quanyan.travel.biz.BizTravelOrderService;
import com.quanyan.travel.component.cache.MyRedisTemplate;
import com.quanyan.travel.constants.OutdoorConstants;
import com.quanyan.travel.entity.*;
import com.quanyan.travel.entity.vo.*;
import com.quanyan.travel.enums.ErrorEnum;
import com.quanyan.travel.mapper.*;
import com.quanyan.travel.service.TravelAdminService;
import com.quanyan.travel.service.TravelCacheService;
import com.quanyan.travel.service.TravelMsgService;
import com.quanyan.user.response.RespUserInfoBase;
import com.quanyan.user.serviceFacade.UserServiceFacade;
import org.apache.commons.collections.CollectionUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;

/**
 * 订单Service
 * Created by yangyun on 2016/4/19.
 */


@Service("travelAdminService")
public class TravelAdminServiceImpl implements TravelAdminService {

    private static Logger logger = Logger.getLogger(TravelAdminServiceImpl.class);

    @Autowired
    private BizTravelOrderService bizTravelOrderService;

    @Autowired
    private TbIndexBannerMapper indexBannerMapper;

    @Autowired
    private TbOutdoorCategoryMapper outdoorCategoryMapper;

    @Autowired
    private TbOutdoorMasterMapper outdoorMasterMapper;

    @Autowired
    private TbOutdoorActivityMapper tbOutdoorActivityMapper;

    @Autowired
    private TbOutdoorDiscountMapper tbOutdoorDiscountMapper;

    @Autowired
    private TbOutdoorThemeMapper themeMapper;

    @Autowired
    private TbOutdoorDestinationMapper tbOutdoorDestinationMapper;

    @Autowired
    private TbOutdoorMasterMapper tbOutdoorMasterMapper;

    @Autowired
    private TbOutdoorActivityDestinationMapper tbOutdoorActivityDestinationMapper;

    @Autowired
    private TbOutdoorProductMapper tbOutdoorProductMapper;

    @Autowired
    private TbOutdoorCategoryMapper tbOutdoorCategoryMapper;

    @Autowired
    private TbOutdoorThemeMapper tbOutdoorThemeMapper;

    @Autowired
    private TbCityMapper tbCityMapper;

    @Autowired
    private TravelCacheService travelCacheService;

    @Autowired
    private VOrderActivityMapper orderActivityMapper;

    @Autowired
    UserServiceFacade userServiceFacade;

    @Autowired
    private TbOutdoorActivityDescMapper outdoorActivityDescMapper;

    @Autowired
    private VTravelUserDefinedMapper travelUserDefinedMapper;

    @Autowired
    private UserCollectServiceFacade userCollectServiceFacade;

    @Autowired
    private TravelMsgService travelMsgService;

    @Autowired
    private TbOutdoorSignUserMapper signUserMapper;

    @Autowired
    private TbIndexBannerMapper tbIndexBannerMapper;

    @Autowired
    private MyRedisTemplate redisTemplate;

    @Value("${cache_expire_time}")
    private Integer cache_expire_time;

    @Override
    public APIResponse<List<Banner>> allBanner() {
        List<Banner> banners = Lists.newArrayList();
        TbIndexBannerExample example = new TbIndexBannerExample();
        TbIndexBannerExample.Criteria criteria = example.createCriteria();
        criteria.andIsDeleteEqualTo((byte) 0);
        example.setOrderByClause(" sequence ");
        List<TbIndexBanner> tbIndexBanners = indexBannerMapper.selectByExample(example);
        if (tbIndexBanners != null && tbIndexBanners.size() > 0) {
            for (TbIndexBanner indexBanner : tbIndexBanners) {
                Banner banner = new Banner();
                BeanUtils.copyProperties(indexBanner, banner);
                banner.setIndex(indexBanner.getSequence());
                banner.setActivityId(indexBanner.getLinkId());
                banner.setType((int) indexBanner.getType());
                banners.add(banner);
            }
        }
        return APIResponse.returnSuccess(banners);
    }

    @Override
    public APIResponse<Integer> deleteBanner(Integer id) {
        TbIndexBanner tbIndexBanner = new TbIndexBanner();
        tbIndexBanner.setIsDelete(OutdoorConstants.IS_DELETE);
        TbIndexBannerExample example = new TbIndexBannerExample();
        TbIndexBannerExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(id);
        Integer result = indexBannerMapper.updateByExampleSelective(tbIndexBanner, example);
        try {
            travelCacheService.updateBanner();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return APIResponse.returnSuccess(result);
        }
    }

    @Override
    public APIResponse<Integer> addBanner(Banner banner) {
        TbIndexBanner indexBanner = new TbIndexBanner();
        BeanUtils.copyProperties(banner, indexBanner);
        indexBanner.setSequence(banner.getIndex());
        indexBanner.setLinkId(banner.getActivityId());
        indexBanner.setType((byte) banner.getType().intValue());
        indexBanner.setIsDelete((byte) 0);
        indexBanner.setCreateTime(new Date());
        indexBanner.setUpdateTime(new Date());
        Integer result = indexBannerMapper.insertSelective(indexBanner);
        try {
            travelCacheService.updateBanner();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return APIResponse.returnSuccess(result);
        }
    }

    @Override
    public APIResponse<Banner> selectActivityForBanner(String activityName) {
        Banner banner = new Banner();
        TbOutdoorActivityExample example = new TbOutdoorActivityExample();
        TbOutdoorActivityExample.Criteria criteria = example.createCriteria();
        criteria.andIsDeleteEqualTo(OutdoorConstants.IS_NOT_DELETE);
        criteria.andNameLike("%" + activityName +"%");
        criteria.andIsIndexShowEqualTo(OutdoorConstants.INDEX_SHOW_TRUE);
        List<TbOutdoorActivity> outdoorActivityList = tbOutdoorActivityMapper.selectByExample(example);
        if (null != outdoorActivityList && outdoorActivityList.size() >0){
            Integer activityId = outdoorActivityList.get(0).getId();
            //查询该活动是否有可用场次
            TbOutdoorProductExample example_product = new TbOutdoorProductExample();
            TbOutdoorProductExample.Criteria criteria_product = example_product.createCriteria();
            criteria_product.andActivityIdEqualTo(activityId);
            criteria_product.andIsDeleteEqualTo(OutdoorConstants.IS_NOT_DELETE);
            Date date = new Date();
            criteria_product.andStartTimeGreaterThan(date);
            criteria_product.andSignEndTimeGreaterThan(date);
            criteria_product.andInventoryLevelGreaterThan(0);
            List<TbOutdoorProduct> outdoorProductList = tbOutdoorProductMapper.selectByExample(example_product);
            if (null != outdoorActivityList && outdoorActivityList.size() > 0){
                banner.setActivityId(activityId);
                banner.setType(1);
                banner.setName(activityName);
            }
        }
        return APIResponse.returnSuccess(banner);
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED, rollbackFor={Exception.class})
    public APIResponse setBannerPriority(ReqBannerPriorityList priorityList) {
        if (null == priorityList || null == priorityList.getReqBannerPriorityList() || priorityList.getReqBannerPriorityList().size() == 0){
            return APIResponse.returnFail("传入对象不能为空！");
        }
        for (ReqBannerPriority priority :priorityList.getReqBannerPriorityList()){
            TbIndexBanner tbIndexBanner = tbIndexBannerMapper.selectByPrimaryKey(priority.getId());
            tbIndexBanner.setSequence(priority.getPriority());
            tbIndexBannerMapper.updateByPrimaryKey(tbIndexBanner);
        }
        return APIResponse.returnSuccess();
    }

    @Override
    public APIResponse editBanner(Banner banner) {
        if (null == banner){
            return APIResponse.returnFail("传入对象不能为空！");
        }
        if(null == banner.getId()){
            return APIResponse.returnFail("传入banner的ID不能为空！");
        }

        TbIndexBanner tbIndexBanner = tbIndexBannerMapper.selectByPrimaryKey(banner.getId());
        if (null == tbIndexBanner){
            return APIResponse.returnFail("请检查传入的banner图Id，数据库不存在该记录！");
        }
        BeanUtils.copyProperties(banner,tbIndexBanner);
        tbIndexBannerMapper.updateByPrimaryKey(tbIndexBanner);
        try {
            travelCacheService.updateBanner();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return APIResponse.returnSuccess("记录 "+ banner.getId() + " 更新成功");
    }

    @Override
    public APIResponse<Integer> selectRecommendNum() {
        Integer result = 0;
        TbIndexBannerExample example = new TbIndexBannerExample();
        TbIndexBannerExample.Criteria criteria = example.createCriteria();
        criteria.andIsDeleteEqualTo(OutdoorConstants.IS_NOT_DELETE);
        List<TbIndexBanner> indexBannerList = tbIndexBannerMapper.selectByExample(example);
        if (null != indexBannerList){
            result = indexBannerList.size();
        }
        return APIResponse.returnSuccess(result);
    }

    @Override
    public APIResponse<PageObj<List<Destination>>> allDestinations(ReqAllDestination request) {
        TbOutdoorDestinationExample example = new TbOutdoorDestinationExample();
        TbOutdoorDestinationExample.Criteria criteria = example.createCriteria();
        criteria.andIsDeleteEqualTo(OutdoorConstants.IS_DELETE_FALSE);
        if (null != request.getCityId()) {
            criteria.andCityIdEqualTo(request.getCityId());
        }
        Integer total = tbOutdoorDestinationMapper.countByExample(example);
        example.setOrderByClause(" create_time desc");
        example.setPage(
            new Page(request.getPageNo() * request.getPageSize(), request.getPageSize(), total));
        List<TbOutdoorDestination> tbOutdoorDestinations =
            tbOutdoorDestinationMapper.selectByExample(example);
        if (tbOutdoorDestinations != null && tbOutdoorDestinations.size() > 0) {
            List<Destination> destinationList = new ArrayList<>();
            for (TbOutdoorDestination vOutdoorDestination : tbOutdoorDestinations) {
                Destination destination = new Destination();
                BeanUtils.copyProperties(vOutdoorDestination, destination);
                destinationList.add(destination);
            }
            return APIResponse.returnSuccess(
                PageObj.create(total, request.getPageNo(), request.getPageSize(), destinationList));
        } else {
            return APIResponse.returnFail("查询目的地列表失败！");
        }
    }

    @Override
    public APIResponse<List<Category>> allCategories() {
        List<Category> categoryList = Lists.newArrayList();
        TbOutdoorCategoryExample example = new TbOutdoorCategoryExample();
        TbOutdoorCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andIsDeleteEqualTo(OutdoorConstants.IS_DELETE_FALSE);
        List<TbOutdoorCategory> tbOutdoorCategories =
            outdoorCategoryMapper.selectByExample(example);
        if (tbOutdoorCategories != null && tbOutdoorCategories.size() > 0) {
            for (TbOutdoorCategory tbOutdoorCategory : tbOutdoorCategories) {
                Category category = new Category();
                BeanUtils.copyProperties(tbOutdoorCategory, category);
                category.setCategoryId(tbOutdoorCategory.getId());
                category.setExtendName(tbOutdoorCategory.getExtendName());
                categoryList.add(category);
            }
            return APIResponse.returnSuccess(categoryList);
        } else {
            return APIResponse.returnFail("查询玩法列表失败！");
        }
    }

    @Override
    public APIResponse<?> createMaster(ReqMaster reqMaster) {
        TbOutdoorMaster master = new TbOutdoorMaster();
        BeanUtils.copyProperties(master, reqMaster);
        tbOutdoorMasterMapper.insertSelective(master);
        return APIResponse.returnSuccess();
    }

    @Override
    public APIResponse<?> activityMsg() {
        return APIResponse.returnSuccess(new RespActivityApplyMsgAndProvideParam());
    }

    @Override
    public APIResponse<?> createDestination(ReqDestination reqDestination) {
        TbOutdoorDestination destination = new TbOutdoorDestination();
        BeanUtils.copyProperties(reqDestination, destination);
        tbOutdoorDestinationMapper.insertSelective(destination);
        return APIResponse.returnSuccess();
    }

    @Override
    public APIResponse<PageObj<List<MasterUser>>> getMasterUser(ReqMasterList reqMasterList) {
        List<MasterUser> masterUsers = Lists.newArrayList();
        TbOutdoorMasterExample example = new TbOutdoorMasterExample();
        TbOutdoorMasterExample.Criteria criteria = example.createCriteria();
        example.setOrderByClause(" update_time DESC ");
        criteria.andIsDeleteEqualTo(OutdoorConstants.IS_NOT_DELETE);
        if (!StringUtils.isEmpty(reqMasterList.getKey())) {
            criteria.andNameLike("%" + reqMasterList.getKey() + "%");
        }
        if (!StringUtils.isEmpty(reqMasterList.getMobile())) {
            criteria.andMobileEqualTo(reqMasterList.getMobile());
        }
        Integer total = outdoorMasterMapper.countByExample(example);
        if (total == 0) {
            masterUsers = null;
            return APIResponse.returnSuccess(PageObj.create(total, reqMasterList.getPageNo(), reqMasterList.getPageSize(), masterUsers));
        }
        example.setPage(new Page(reqMasterList.getPageNo() * reqMasterList.getPageSize(), reqMasterList.getPageSize()));
        List<TbOutdoorMaster> list = outdoorMasterMapper.selectByExample(example);
        if (null != list && list.size() > 0) {
            for (TbOutdoorMaster outdoorMaster : list) {
                MasterUser masterUser = new MasterUser();
                BeanUtils.copyProperties(outdoorMaster, masterUser);
                masterUsers.add(masterUser);
            }
            return APIResponse.returnSuccess(PageObj.create(total, reqMasterList.getPageNo(), reqMasterList.getPageSize(), masterUsers));
        } else {
            return APIResponse.returnSuccess(PageObj.create(total, reqMasterList.getPageNo(), reqMasterList.getPageSize(), masterUsers));
        }
    }

    @Override
    public APIResponse getUserByMobile(String mobile) {
        if(!org.apache.commons.lang3.StringUtils.isBlank(mobile)){
            RespUserInfoBase userInfoBase = userServiceFacade.queryUserInfoByPhone(mobile).getData();
            if(userInfoBase != null){
                if(this.isExisted(userInfoBase.getUid())){
                    return APIResponse.returnFail("该用户已经是领队了，请重新换一个用户");
                }
                return APIResponse.returnSuccess(userInfoBase);
            }
        }
        return APIResponse.returnFail("该用户不存在");
    }

    private boolean isExisted(Integer uid){
        if(null != uid){
            TbOutdoorMasterExample tbOutdoorMasterExample = new TbOutdoorMasterExample();
            TbOutdoorMasterExample.Criteria criteria = tbOutdoorMasterExample.createCriteria();
            criteria.andUidEqualTo(uid);
            List<TbOutdoorMaster> masterList= outdoorMasterMapper.selectByExample(tbOutdoorMasterExample);
            if (null != masterList && masterList.size() >0)
                return true;
        }
        return false;
    }

    @Override
    public APIResponse createMasterUser(ReqCreateMasterUser reqCreateMasterUser) {
        TbOutdoorMaster tbOutdoorMaster = new TbOutdoorMaster();
        tbOutdoorMaster.setUid(reqCreateMasterUser.getUid());
        tbOutdoorMaster.setName(reqCreateMasterUser.getName());
        tbOutdoorMaster.setIcon(reqCreateMasterUser.getIcon());
        tbOutdoorMaster.setMobile(reqCreateMasterUser.getMobile());
        tbOutdoorMaster.setIsDelete(OutdoorConstants.IS_NOT_DELETE);
        outdoorMasterMapper.insertSelective(tbOutdoorMaster);
        return APIResponse.returnSuccess();
    }

    @Override
    public APIResponse updateMasterUser(MasterUser masterUser) {
        TbOutdoorMaster tbOutdoorMaster = new TbOutdoorMaster();
        BeanUtils.copyProperties(masterUser,tbOutdoorMaster);
        outdoorMasterMapper.updateByPrimaryKeySelective(tbOutdoorMaster);
        return APIResponse.returnSuccess();
    }


    @Override
    public APIResponse delMasterUser(Integer masterId) {
        TbOutdoorMaster tbOutdoorMaster = new TbOutdoorMaster();
        tbOutdoorMaster.setId(masterId);
        tbOutdoorMaster.setIsDelete(OutdoorConstants.IS_DELETE);
        outdoorMasterMapper.updateByPrimaryKeySelective(tbOutdoorMaster);
        return APIResponse.returnSuccess();
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public APIResponse<Integer> createActivity(ReqCreateActivity reqCreateActivity) {
        //活动的时间 的简单校验
        TbOutdoorActivity activity = this.saveActivity(reqCreateActivity);
        TbOutdoorActivityDesc desc = new TbOutdoorActivityDesc();
        desc.setOutdoorActivityId(activity.getId());
        desc.setBodyDesc(reqCreateActivity.getBodyDesc());
        desc.setAttentionDesc(reqCreateActivity.getAttentionDesc());
        bizTravelOrderService.insertTbOutdoorActivityDesc(desc);

        //目的地插入
        for (Integer integer : reqCreateActivity.getOutdoorDestinationIds()) {
            TbOutdoorActivityDestination destion = new TbOutdoorActivityDestination();
            destion.setActivityId(activity.getId());
            destion.setDestinationId(integer);
            tbOutdoorActivityDestinationMapper.insertSelective(destion);
        }


        //更新折扣信息
        Gson gson = new Gson();
        if (null != reqCreateActivity.getDiscountParameters()
            && reqCreateActivity.getDiscountParameters().size() > 0) {
            TbOutdoorDiscount discount = new TbOutdoorDiscount();
            discount.setActivityId(activity.getId());
            discount.setName("活动:" + activity.getName() + "的折扣");
            discount.setDiscountParameter(gson.toJson(reqCreateActivity.getDiscountParameters()));
            bizTravelOrderService.insertTbOutdoorDiscount(discount);
        }
        //更新领队(不一样)
        if (null != reqCreateActivity.getMasterIds()
            && reqCreateActivity.getMasterIds().size() > 0) {
            for (Integer integer : reqCreateActivity.getMasterIds()) {
                TbOutdoorActivityMaster tbOutdoorActivityMaster = new TbOutdoorActivityMaster();
                tbOutdoorActivityMaster.setMasterUid(integer);
                tbOutdoorActivityMaster.setActivityId(activity.getId());
                bizTravelOrderService.insertTbOutdoorActivityMaster(tbOutdoorActivityMaster);
            }
        }
        if (null == activity) {
            return APIResponse.returnFail("保存活动失败！");
        } else {
            if (reqCreateActivity.getType() == OutdoorConstants.THEME_ACTIVITY_YES) {
                //更新场次
                if (null != reqCreateActivity.getReqCreateProducts()
                    && reqCreateActivity.getReqCreateProducts().size() > 0) {
                    for (ReqCreateProduct product : reqCreateActivity.getReqCreateProducts()) {
                        TbOutdoorTheme theme = new TbOutdoorTheme();
                        theme.setActivityId(activity.getId());
                        theme.setProductId(product.getId());
                        bizTravelOrderService.insertTbOutdoorTheme(theme);
                    }
                }
            } else if (reqCreateActivity.getType() == OutdoorConstants.THEME_ACTIVITY_NO) {
                //更新场次
                if (null != reqCreateActivity.getReqCreateProducts()
                    && reqCreateActivity.getReqCreateProducts().size() > 0) {
                    for (ReqCreateProduct product : reqCreateActivity.getReqCreateProducts()) {
                        TbOutdoorProduct tbOutdoorProduct = new TbOutdoorProduct();
                        tbOutdoorProduct.setActivityId(activity.getId());
                        tbOutdoorProduct.setStartTime(new Date(product.getStartTime()));
                        tbOutdoorProduct.setEndTime(DateUtils
                            .addTime(new Date(product.getStartTime()), activity.getActivityTime(),
                                DateUtils.TIME_TYPE_HOUR));
                        tbOutdoorProduct.setSignStartTime(new Date(product.getSignStartTime()));
                        tbOutdoorProduct.setSignEndTime(new Date(product.getSignEndTime()));
                        tbOutdoorProduct.setLimitNum(product.getLimitNum());
                        tbOutdoorProduct.setInventoryLevel(product.getLimitNum());
                        tbOutdoorProduct.setPrice(product.getPrice());
                        tbOutdoorProduct.setDisplayPrice(product.getDisplayPrice());
                        tbOutdoorProduct.setMinNum(product.getMinNum());
                        bizTravelOrderService.insertTbOutdoorProduct(tbOutdoorProduct);
                    }
                }
            } else {
            }
            return APIResponse.returnSuccess();
        }
    }

    @Override
    public TbOutdoorActivity saveActivity(ReqCreateActivity reqCreateActivity) {
        TbOutdoorActivity activity = new TbOutdoorActivity();
        activity.setType(reqCreateActivity.getType());
        activity.setName(reqCreateActivity.getName());
        activity.setExtendName(reqCreateActivity.getExtendName());
        activity.setAddress(reqCreateActivity.getAddress());
        activity.setLng(reqCreateActivity.getLng());
        activity.setLat(reqCreateActivity.getLat());
        activity.setLimitNum(reqCreateActivity.getLimitNum());
        activity.setStartTime(reqCreateActivity.getStartTime());
        activity.setOutdoorCategoryId(reqCreateActivity.getOutdoorCategoryId());
        activity.setTag(reqCreateActivity.getTag());
        activity.setPrice(reqCreateActivity.getPrice());
        activity.setMinNum(reqCreateActivity.getMinNum());
        activity.setActivityTime(reqCreateActivity.getActivityTime());
        Gson gson = new Gson();
        activity.setParameter(gson.toJson(reqCreateActivity.getActivityParameter()));
        activity.setSignParameter(gson.toJson(reqCreateActivity.getApplyInfoParameter()));
        activity.setMultiMedia(gson.toJson(reqCreateActivity.getMultiMedia()));
        return bizTravelOrderService.insertTbOutdoorActivity(activity);
    }

    @Override
    public APIResponse<?> createProduct(ReqCreateProduct req) {
        TbOutdoorActivity activity = bizTravelOrderService.queryActivityById(req.getActivityId());
        TbOutdoorProduct tbOutdoorProduct = new TbOutdoorProduct();
        tbOutdoorProduct.setActivityId(req.getActivityId());
        tbOutdoorProduct.setStartTime(new Date(req.getStartTime()));
        tbOutdoorProduct.setEndTime(DateUtils
            .addTime(new Date(req.getStartTime()), activity.getActivityTime(),
                DateUtils.TIME_TYPE_HOUR));
        tbOutdoorProduct.setSignStartTime(new Date(req.getSignStartTime()));
        tbOutdoorProduct.setSignEndTime(DateUtils
                .addTime(new Date(req.getStartTime()), activity.getActivityTime(),
                        DateUtils.TIME_TYPE_HOUR));
        tbOutdoorProduct.setLimitNum(req.getLimitNum());
        tbOutdoorProduct.setInventoryLevel(req.getLimitNum());
        tbOutdoorProduct.setPrice(req.getPrice());
        tbOutdoorProduct.setDisplayPrice(req.getDisplayPrice());
        tbOutdoorProduct.setMinNum(activity.getMinNum());

        TbOutdoorProduct product = bizTravelOrderService.insertTbOutdoorProduct(tbOutdoorProduct);
        if (null == product || product.getId() < 1) {
            return APIResponse.returnFail(ErrorEnum.ERROR_DBERROR.getLabel());
        }
        //同时更新状态,活动功能
        TbOutdoorActivity activityTemp = new TbOutdoorActivity();
        activityTemp.setId(req.getActivityId());
        activityTemp.setIsDelete(OutdoorConstants.IS_NOT_DELETE);
        tbOutdoorActivityMapper.updateByPrimaryKeySelective(activityTemp);


        //推荐给喜欢的用户
        ReqUserCollectQuery reqUserCollectQuery = new ReqUserCollectQuery();
        reqUserCollectQuery.setSystemCode(UserCenterConstants.SystemCode.SYSTEM_OUTDOOR);
        reqUserCollectQuery.setBusinessCode(UserCenterConstants.BusinessCode.BUSINESS_OUTDOOR_CM);
        reqUserCollectQuery.setBusinessId(req.getActivityId());
        reqUserCollectQuery.setPageNum(0);
        reqUserCollectQuery.setPageSize(100);
        PageObj<List<Integer>> listPageObj = new PageObj<>();
        TbOutdoorActivity tbOutdoorActivity =
            bizTravelOrderService.queryActivityById(req.getActivityId());
        try {
            listPageObj =
                userCollectServiceFacade.queryUsersByBusiness(reqUserCollectQuery).getData();
            Map<String, Object> params = new HashMap<>();
            params.put("activityId", req.getActivityId());
            params.put("activityName", tbOutdoorActivity.getActivityTime());
            travelMsgService.userFavActivityPublishMsg(listPageObj.getItems(), params,
                tbOutdoorActivity.getName());

        } catch (Exception e) {
            logger.error("dubbo服务：queryUsersByBusiness出错！", e);
        }

        return APIResponse.returnSuccess();
    }

    @Override
    public APIResponse<PageObj<List<ActivityList>>> getActivityList(ReqActivityList req) {
        List<ActivityList> result = new ArrayList<>();
        List<TbOutdoorActivity> outdoorActivityList = new ArrayList<>();
        TbOutdoorActivityExample example = new TbOutdoorActivityExample();
        TbOutdoorActivityExample.Criteria criteria = example.createCriteria();
        if (null != req.getType()) {
            criteria.andTypeEqualTo(req.getType());
        }
        if (!StringUtils.isEmpty(req.getKey())) {
            criteria.andNameLike("%" + req.getKey() + "%");
        }
        if (null != req.getOutdoorCategorId()) {
            criteria.andOutdoorCategoryIdEqualTo(req.getOutdoorCategorId());
        }
        if (req.getIsDelete() != null) {
            criteria.andIsDeleteEqualTo(req.getIsDelete());
        }
        int total = tbOutdoorActivityMapper.countByExample(example);
        if (total == 0) {
            return APIResponse.returnSuccess(PageObj.create(0, req.getPageNo(), req.getPageSize(), result));
        }
        example.setOrderByClause("create_time desc");
        example.setPage(new Page(req.getPageNo() * req.getPageSize(), req.getPageSize()));
        outdoorActivityList = tbOutdoorActivityMapper.selectByExample(example);
        for (TbOutdoorActivity outdoorActivity : outdoorActivityList){
            ActivityList activity = new ActivityList();
            BeanUtils.copyProperties(outdoorActivity,activity);
            activity.setIsRecommend(this.isRecommend(activity.getId()));
            result.add(activity);
        }
        return APIResponse.returnSuccess(PageObj.create(total, req.getPageNo(), req.getPageSize(), result));
    }

    @Override
    public Byte isRecommend(Integer activityId) {
        TbIndexBannerExample example = new TbIndexBannerExample();
        TbIndexBannerExample.Criteria criteria = example.createCriteria();
        criteria.andLinkIdEqualTo(activityId);
        criteria.andIsDeleteEqualTo(OutdoorConstants.IS_NOT_DELETE);
        List<TbIndexBanner> indexBannerList = tbIndexBannerMapper.selectByExample(example);
        if (null != indexBannerList && indexBannerList.size() >0){
            return OutdoorConstants.IS_RECOMMEND;
        }
        return OutdoorConstants.IS_NOT_RECOMMEND;
    }

    @Override
    public APIResponse<RespProductMsg> getProductListByActivityId(Integer activityId) {
        RespProductMsg respProductMsg = new RespProductMsg();
        TbOutdoorProductExample example = new TbOutdoorProductExample();
        TbOutdoorProductExample.Criteria criteria = example.createCriteria();
        criteria.andActivityIdEqualTo(activityId);
        criteria.andIsDeleteEqualTo((byte) 0);
        criteria.andStartTimeGreaterThan(new Date());   //只展示未开始的活动
        respProductMsg.setActivityId(activityId);
        respProductMsg.setProductList(tbOutdoorProductMapper.selectByExample(example));
        TbOutdoorActivityDestinationExample example1 = new TbOutdoorActivityDestinationExample();
        TbOutdoorActivityDestinationExample.Criteria criteria1 = example1.createCriteria();
        criteria1.andActivityIdEqualTo(activityId);
        List<TbOutdoorActivityDestination> list =
            tbOutdoorActivityDestinationMapper.selectByExample(example1);
        if (null == list || list.size() < 1) {
            return APIResponse.returnSuccess(null);
        }

        TbOutdoorDestination destination =
            bizTravelOrderService.queryTbOutdoorDestinationById(list.get(0).getDestinationId());
        if (null != destination) {
            respProductMsg.setDestination(destination.getAddress());
            respProductMsg.setDestinationId(destination.getId());
        }
        return APIResponse.returnSuccess(respProductMsg);
    }

    @Override
    public void refreshTravelActivityStatus() {
        List<Integer> activityIds = bizTravelOrderService.selectExpireActivity();
        if (null != activityIds && activityIds.size() > 0) {
            bizTravelOrderService.updateTbOutdoorActivity(activityIds);   //刷新活动状态
            bizTravelOrderService.updateTbOutdoorProduct(activityIds);    //刷新场次状态
        }
        travelUserDefinedMapper.updateExpireThemeActivity();
        travelUserDefinedMapper.updateExpireTheme();
        List<Integer> notShowActivity = bizTravelOrderService.indexNotShow();
        if (null != notShowActivity && notShowActivity.size() > 0) {
            bizTravelOrderService.updateNotShwoActivity(notShowActivity);   //首页不显示状态
        }
    }

    @Override
    public void refreshTravelThemeActivityStatus() {
        List<TbOutdoorActivity> activityList =
            bizTravelOrderService.selectNotExpireThemeActivityList();
        if (null != activityList && activityList.size() > 0) {
            List<Integer> activityIds = new ArrayList<>();
            for (TbOutdoorActivity tbOutdoorActivity : activityList) {
                activityIds.add(tbOutdoorActivity.getId());
            }
            TbOutdoorThemeExample example = new TbOutdoorThemeExample();
            TbOutdoorThemeExample.Criteria criteria = example.createCriteria();
            criteria.andActivityIdIn(activityIds);
            criteria.andIsDeleteEqualTo(OutdoorConstants.IS_NOT_DELETE);
            List<TbOutdoorTheme> themes = tbOutdoorThemeMapper.selectByExample(example);
            if (null != themes && themes.size() > 0) {
                Map<Integer, List<Integer>> map = new HashMap<>();
                Set<Integer> allProducts = new HashSet<>();
                List<Integer> products = new ArrayList<>();
                for (TbOutdoorTheme theme : themes) {
                    allProducts.add(theme.getProductId());
                    products = map.get(theme.getActivityId());
                    if (products == null || products.size() < 1) {
                        products = new ArrayList<>();
                    }
                    products.add(theme.getProductId());
                    map.put(theme.getActivityId(), products);
                }

                TbOutdoorProductExample example1 = new TbOutdoorProductExample();
                TbOutdoorProductExample.Criteria criteria1 = example1.createCriteria();
                criteria1.andIdIn(new ArrayList<Integer>(allProducts));
                List<TbOutdoorProduct> tbOutdoorProducts =
                    tbOutdoorProductMapper.selectByExample(example1);

                List<Integer> expireProducts = new ArrayList<>();
                if (null != tbOutdoorProducts && tbOutdoorProducts.size() > 0) {
                    for (TbOutdoorProduct tbOutdoorProduct : tbOutdoorProducts) {
                        if (tbOutdoorProduct.getIsDelete().equals(OutdoorConstants.IS_DELETE)) {
                            expireProducts.add(tbOutdoorProduct.getId());
                        }
                    }
                }
                if (null != expireProducts && expireProducts.size() > 0) {
                    TbOutdoorThemeExample example2 = new TbOutdoorThemeExample();
                    TbOutdoorThemeExample.Criteria criteria2 = example2.createCriteria();
                    criteria2.andProductIdIn(expireProducts);
                    criteria2.andIsDeleteEqualTo(OutdoorConstants.IS_NOT_DELETE);
                    TbOutdoorTheme theme = new TbOutdoorTheme();
                    theme.setIsDelete(OutdoorConstants.IS_DELETE);
                    tbOutdoorThemeMapper.updateByExampleSelective(theme, example);      //更新过期场次

                    List<Integer> expireActivityIds = new ArrayList<>();
                    for (Map.Entry<Integer, List<Integer>> entry : map.entrySet()) {
                        List<Integer> ids = entry.getValue();
                        if (null != ids && ids.size() > 0) {
                            ids.retainAll(expireProducts);
                            if (null == ids && ids.size() < 1) {
                                expireActivityIds.add(entry.getKey());
                            }
                        }
                    }
                    TbOutdoorActivityExample example3 = new TbOutdoorActivityExample();
                    TbOutdoorActivityExample.Criteria criteria3 = example3.createCriteria();
                    criteria3.andIdIn(expireActivityIds);
                    TbOutdoorActivity activity = new TbOutdoorActivity();
                    activity.setIsDelete(OutdoorConstants.IS_DELETE);
                    tbOutdoorActivityMapper
                        .updateByExampleSelective(activity, example3);      //更新过期场次
                }

            }
        }
    }

    @Override
    public List<ProductDetail> queryUsableProductDetail(ReqUsableProduct reqUseableProduct) {
        return bizTravelOrderService
            .searchUsableProductDetail(reqUseableProduct.getKey(), reqUseableProduct.getPageNum(),
                reqUseableProduct.getPageSize());
    }

    @Override
    public void updateDestination(TbOutdoorDestination tbOutdoorDestination) {
        tbOutdoorDestinationMapper.updateByPrimaryKeySelective(tbOutdoorDestination);
    }

    @Override
    public void insertOutdoorCategory(TbOutdoorCategory tbOutdoorCategory) {
        tbOutdoorCategoryMapper.insertSelective(tbOutdoorCategory);
    }

    @Override
    public void updateOutdoorCategory(TbOutdoorCategory tbOutdoorCategory) {
        tbOutdoorCategoryMapper.updateByPrimaryKeySelective(tbOutdoorCategory);
    }

    @Override
    public APIResponse activityUndercarriage(Integer activityId) {
        TbOutdoorActivity activityTemp = tbOutdoorActivityMapper.selectByPrimaryKey(activityId);
        if (null == activityTemp || activityTemp.getId() < 1) {
            return APIResponse.returnFail("活动不存在");
        }
        TbOutdoorProductExample example = new TbOutdoorProductExample();
        TbOutdoorProductExample.Criteria criteria = example.createCriteria();
        criteria.andActivityIdEqualTo(activityId);
        criteria.andIsDeleteEqualTo(OutdoorConstants.IS_NOT_DELETE);
        List<TbOutdoorProduct> products = tbOutdoorProductMapper.selectByExample(example);
        if (null != products && products.size() > 0) {
            for (TbOutdoorProduct product : products) {
                if (product.getSignStartTime().before(new Date()) && product.getEndTime()
                    .after(new Date())) {
                    return APIResponse.returnFail("处于报名期间,进行中的活动功能下架");
                }
            }
        }
        TbOutdoorActivity activity = new TbOutdoorActivity();
        activity.setId(activityId);
        activity.setIsDelete(OutdoorConstants.ACTIVITY_UNDERCARRIAGE);
        tbOutdoorActivityMapper.updateByPrimaryKeySelective(activity);
        if (activityTemp.getType() == OutdoorConstants.THEME_ACTIVITY_NO) {   //非季度活动下架下面的场次
            for (TbOutdoorProduct product : products) {
                TbOutdoorProduct productTemp = new TbOutdoorProduct();
                productTemp.setId(product.getId());
                productTemp.setIsDelete(OutdoorConstants.ACTIVITY_UNDERCARRIAGE);
                tbOutdoorProductMapper.updateByPrimaryKeySelective(productTemp);
            }
        }
        return APIResponse.returnSuccess();
    }

    @Override
    public APIResponse updateActivity(ReqCreateActivity createActivity) {

        TbOutdoorActivity activity = new TbOutdoorActivity();
        activity.setId(createActivity.getId());
        activity.setName(createActivity.getName());

        return null;
    }

    @Override
    public APIResponse getProductList(Integer activityId) {
        TbOutdoorActivity activity = tbOutdoorActivityMapper.selectByPrimaryKey(activityId);
        RespAppActivityDetail detail = new RespAppActivityDetail();
        detail.setActivityId(activityId);
        detail.setName(activity.getName());
        detail.setExtendName(activity.getExtendName());
        detail.setType(activity.getType());
        detail.setPrice(new BigDecimal(String.valueOf(activity.getPrice())));
        detail.setMinNum(activity.getMinNum());
        detail.setMaxNum(activity.getLimitNum());
        detail.setTimeLength(activity.getActivityTime());
        detail.setIsDelete(activity.getIsDelete());
        List<Integer> ids = new ArrayList<>();

        TbOutdoorProductExample example = new TbOutdoorProductExample();
        TbOutdoorProductExample.Criteria criteria = example.createCriteria();
        criteria.andActivityIdEqualTo(activityId);
        criteria.andIsDeleteEqualTo(OutdoorConstants.IS_NOT_DELETE);
        List<TbOutdoorProduct> products = tbOutdoorProductMapper.selectByExample(example);
        for (TbOutdoorProduct product : products) {
            ids.add(product.getId());
        }
        List<RespPriceCalendar> result = new ArrayList<>();
        if (activity.getType() == 0) {
            if (null != ids && ids.size() > 0) {
                List<ProductDetail> productDetails =
                    bizTravelOrderService.queryProductDetailByIds(ids);
                if (null != productDetails && productDetails.size() > 0) {
                    for (ProductDetail productDetail : productDetails) {
                        RespPriceCalendar respPriceCalendar = new RespPriceCalendar();
                        respPriceCalendar.setProductId(productDetail.getId());
                        respPriceCalendar.setDisplayPrice(productDetail.getDisplayPrice());
                        respPriceCalendar.setPrice(productDetail.getPrice());
                        respPriceCalendar.setStartTime(productDetail.getStartTime());
                        respPriceCalendar.setEndTime(productDetail.getEndTime());
                        respPriceCalendar.setLimitNum(productDetail.getLimitNum());
                        respPriceCalendar.setInventoryLevel(productDetail.getStorage());
                        respPriceCalendar.setMinNum(productDetail.getMinNum());
                        result.add(respPriceCalendar);
                    }
                }
            }
        } else if (activity.getType() == 1) {
            TbOutdoorThemeExample exampleTemp = new TbOutdoorThemeExample();
            TbOutdoorThemeExample.Criteria criteriaTemp = exampleTemp.createCriteria();

            criteriaTemp.andActivityIdEqualTo(activityId);
            criteriaTemp.andIsDeleteEqualTo(OutdoorConstants.IS_NOT_DELETE);
            List<TbOutdoorTheme> themes = tbOutdoorThemeMapper.selectByExample(exampleTemp);
            if (themes.size() < 1) {
                return APIResponse.returnFail("异常");
            }
            ids.clear();
            for (TbOutdoorTheme theme : themes) {
                ids.add(theme.getProductId());
            }
            TbOutdoorProductExample exampleProduct = new TbOutdoorProductExample();
            TbOutdoorProductExample.Criteria criteriaProduct = exampleProduct.createCriteria();
            criteriaProduct.andIdIn(ids);
            List<TbOutdoorProduct> productList =
                tbOutdoorProductMapper.selectByExample(exampleProduct);
            if (productList.size() < 1) {
                return APIResponse.returnFail("异常");
            }
            for (TbOutdoorProduct product : productList) {
                RespPriceCalendar respPriceCalendar = new RespPriceCalendar();
                respPriceCalendar.setProductId(product.getId());
                respPriceCalendar.setDisplayPrice(product.getDisplayPrice());
                respPriceCalendar.setPrice(product.getPrice());
                respPriceCalendar.setStartTime(product.getStartTime());
                respPriceCalendar.setEndTime(product.getEndTime());
                respPriceCalendar.setLimitNum(product.getLimitNum());
                respPriceCalendar.setInventoryLevel(product.getInventoryLevel());
                respPriceCalendar.setMinNum(product.getMinNum());
                result.add(respPriceCalendar);
            }
        } else {

        }
        detail.setCalendarList(result);
        return APIResponse.returnSuccess(detail);
    }

    @Override
    public APIResponse<RespWebActivityDetail> activityDetail(Integer activityId) {
        List<Integer> ids = new ArrayList<>();
        ids.add(activityId);
        TbOutdoorActivity activity = tbOutdoorActivityMapper.selectByPrimaryKey(activityId);

        TbOutdoorActivityDescExample ee = new TbOutdoorActivityDescExample();
        TbOutdoorActivityDescExample.Criteria cc = ee.createCriteria();
        cc.andOutdoorActivityIdEqualTo(activityId);
        List<TbOutdoorActivityDesc> descList = outdoorActivityDescMapper.selectByExample(ee);
        if (null == descList || descList.size() != 1) {
            return APIResponse.returnFail("活动介绍不存在,活动无效");
        }

        List<Master> masters = bizTravelOrderService.queryOutdoorMaster(ids);
        List<Destination> destinations = new ArrayList<>();

        TbOutdoorActivityDestinationExample example = new TbOutdoorActivityDestinationExample();
        TbOutdoorActivityDestinationExample.Criteria criteria = example.createCriteria();
        criteria.andActivityIdEqualTo(activityId);
        List<TbOutdoorActivityDestination> temp =
            tbOutdoorActivityDestinationMapper.selectByExample(example);
        if (temp.size() < 0) {
            return APIResponse.returnFail("活动不存在目的地,活动无效");
        }
        ids.clear();
        for (TbOutdoorActivityDestination tb : temp) {
            ids.add(tb.getDestinationId());
        }

        TbOutdoorCategory category =
            tbOutdoorCategoryMapper.selectByPrimaryKey(activity.getOutdoorCategoryId());
        TbOutdoorDestinationExample ex = new TbOutdoorDestinationExample();
        TbOutdoorDestinationExample.Criteria cr = ex.createCriteria();
        cr.andIdIn(ids);
        List<TbOutdoorDestination> destinationList = tbOutdoorDestinationMapper.selectByExample(ex);
        if (destinationList.size() < 0) {
            return APIResponse.returnFail("活动不存在目的地,活动无效");
        }
        for (TbOutdoorDestination tb : destinationList) {
            Destination allDestination = new Destination();
            allDestination.setCityId(tb.getCityId());
            allDestination.setName(tb.getCityName());
            allDestination.setAddress(tb.getAddress());
            allDestination.setId(tb.getId());
            destinations.add(allDestination);
        }

        List<Integer> activityIds = new ArrayList<>();
        activityIds.add(activityId);
        List<Master> masterList = bizTravelOrderService.queryOutdoorMaster(activityIds);

        //收藏用户列表
        Gson gson = new Gson();
        RespWebActivityDetail detail = new RespWebActivityDetail();
        detail.setMasterList(masterList);
        detail.setId(activityId);
        detail.setName(activity.getName());
        detail.setExtendName(activity.getExtendName());
        detail.setType(activity.getType());
        detail.setLimitNum(activity.getLimitNum());
        detail.setStartTime(activity.getStartTime());
        detail.setCreateTime(activity.getCreateTime());
        detail.setApplyInfoParameter(
            gson.fromJson(activity.getSignParameter(), ApplyInfoParameter.class));
        detail
            .setActivityParameter(gson.fromJson(activity.getParameter(), ActivityParameter.class));
        detail.setLat(activity.getLat());
        detail.setLng(activity.getLng());
        detail.setAddress(activity.getAddress());
        MultiMedia multiMedia = gson.fromJson(activity.getMultiMedia(), MultiMedia.class);
        detail.setMultiMedia(multiMedia);
        detail.setBody(descList.get(0).getBodyDesc());
        detail.setAttention(descList.get(0).getAttentionDesc());
        detail.setMasterList(masters);
        detail.setDestinationList(destinations);
        detail.setPrice(activity.getPrice());
        detail.setTag(activity.getTag());
        Category categoryTemp = new Category();
        categoryTemp.setId(category.getId());
        categoryTemp.setName(category.getName());
        detail.setCategory(categoryTemp);
        return APIResponse.returnSuccess(detail);
    }

    @Override
    public APIResponse<List<TbCity>> queryAllCity() {
        TbCityExample example = new TbCityExample();
        TbCityExample.Criteria criteria = example.createCriteria();
        criteria.andIsEnableEqualTo(1);
        criteria.andIsActiveEqualTo(1);
        List<TbCity> cities = tbCityMapper.selectByExample(example);
        return APIResponse.returnSuccess(cities);
    }

    @Override
    public void updateLowPrice() {
        TbOutdoorActivityExample example = new TbOutdoorActivityExample();
        TbOutdoorActivityExample.Criteria criteria = example.createCriteria();
        criteria.andIsDeleteEqualTo(OutdoorConstants.IS_NOT_DELETE);
        //不是季度活动
        criteria.andTypeEqualTo(OutdoorConstants.THEME_ACTIVITY_NO);
        List<TbOutdoorActivity> list = tbOutdoorActivityMapper.selectByExample(example);
        if (!CollectionUtils.isEmpty(list)) {
            for (TbOutdoorActivity activity : list) {
                travelUserDefinedMapper.updatePriceActivity(activity.getId());
                travelUserDefinedMapper.updateStartTimeActivity(activity.getId());
            }
        }

        TbOutdoorActivityExample example1 = new TbOutdoorActivityExample();
        TbOutdoorActivityExample.Criteria criteria1 = example1.createCriteria();
        criteria1.andIsDeleteEqualTo(OutdoorConstants.IS_NOT_DELETE);
        criteria1.andTypeEqualTo(OutdoorConstants.THEME_ACTIVITY_YES);
        List<TbOutdoorActivity> list1 = tbOutdoorActivityMapper.selectByExample(example1);
        if (!CollectionUtils.isEmpty(list1)) {
            for (TbOutdoorActivity activity : list1) {
                travelUserDefinedMapper.updatePriceThemeActivity(activity.getId());
                travelUserDefinedMapper.updateThemeMinTime(activity.getId());

            }
        }
    }

    @Override
    public void remindUserPay() {
        List<TbOutdoorOrder> payingOrders = bizTravelOrderService.queryPayingOrder();
        if (null != payingOrders && payingOrders.size() > 0) {
            for (TbOutdoorOrder tbOutdoorOrder : payingOrders) {
                TbOutdoorActivity activity =
                    bizTravelOrderService.queryActivityById(tbOutdoorOrder.getActivityId());
                Map<String, Object> params = new HashMap<>();
                params.put("orderNo", tbOutdoorOrder.getOutdoorOrderNo());
                params.put("orderToken", "BAILINGUP");
                params.put("payId", 1);
                params.put("useAccumulate", false);
                travelMsgService.payRemindMsg(tbOutdoorOrder.getUid(), params, activity.getName());
            }

        }
    }

    @Override
    public APIResponse<PageObj<List<VOrderActivityExt>>> travelOrderList(ReqOrderList request) {
        PageObj<List<VOrderActivityExt>> page = new PageObj<List<VOrderActivityExt>>();
        VOrderActivityExample example = new VOrderActivityExample();
        VOrderActivityExample.Criteria criteria = example.createCriteria();
        if(!org.apache.commons.lang3.StringUtils.isBlank(request.getPhoneNum())){
            RespUserInfoBase userInfoBase = userServiceFacade.queryUserInfoByPhone(request.getPhoneNum()).getData();
            if(userInfoBase != null){
                criteria.andUidEqualTo(userInfoBase.getUid());
            }else{
                return APIResponse.returnSuccess(page);
            }
        }
        if (!org.apache.commons.lang3.StringUtils.isBlank(request.getOrderNo())) {
            criteria.andOutdoorOrderNoEqualTo(request.getOrderNo());
        }
        if (!org.apache.commons.lang3.StringUtils.isBlank(request.getOutdoorName())) {
            criteria.andNameLike(request.getOutdoorName());
        }
        if (null != request.getOutdoorId()) {
            criteria.andActivityIdEqualTo(request.getOutdoorId());
        }
        if (null != request.getActivityType()) {
            criteria.andTypeEqualTo(request.getActivityType().byteValue());
        }
        if(request.getOrderStatus() != null){
            criteria.andOrderDisplayStatusEqualTo(request.getOrderStatus().byteValue());
        }
        Integer totalCount = orderActivityMapper.countByExample(example);
        example.setPage(new Page(request.getPageNum() * request.getPageSize(),request.getPageSize(),totalCount));
        example.setOrderByClause("create_time desc");
        List<VOrderActivity> list = orderActivityMapper.selectByExample(example);
        List<VOrderActivityExt> vOrderActivityExts = new ArrayList<>();
        for(VOrderActivity order : list){
            VOrderActivityExt vOrderActivityExt = new VOrderActivityExt();
            BeanUtils.copyProperties(order,vOrderActivityExt);
            try {
                RespUserInfoBase user = userServiceFacade.queryUserInfo(order.getUid()).getData();
                vOrderActivityExt.setPhoneNum(user.getMobile());
            }catch (Exception e){
                logger.error("查询报名用户手机号失败！");
            }
            vOrderActivityExts.add(vOrderActivityExt);
        }
        page = PageObj.create(totalCount, request.getPageNum(), request.getPageSize(), vOrderActivityExts);
        return APIResponse.returnSuccess(page);
    }

    @Override
    public APIResponse<RespOrderDetails> travelOrderDetail(String orderNo) {
        RespOrderDetails respOrderDetails = new RespOrderDetails();
        VOrderActivityExt vOrderActivityExt = new VOrderActivityExt();
        VOrderActivityExample example = new VOrderActivityExample();
        VOrderActivityExample.Criteria criteria = example.createCriteria();
        criteria.andOutdoorOrderNoEqualTo(orderNo);
        List<VOrderActivity> list = orderActivityMapper.selectByExample(example);
        if(CollectionUtils.isEmpty(list)){
            return APIResponse.returnSuccess(respOrderDetails);
        }
        VOrderActivity orderActivity = list.get(0);
        BeanUtils.copyProperties(orderActivity,vOrderActivityExt);
        vOrderActivityExt.setOrderDetailStatus(orderActivity.getOrderDisplayStatus());
        try {
            RespUserInfoBase user = userServiceFacade.queryUserInfo(vOrderActivityExt.getUid()).getData();
            vOrderActivityExt.setPhoneNum(user.getMobile());
            vOrderActivityExt.setUserName(user.getNickName());
        }catch (Exception e){
            logger.error("查询报名用户手机号失败！");
        }
        respOrderDetails.setOrderMsg(vOrderActivityExt);
        TbOutdoorSignUserExample signUserExample = new TbOutdoorSignUserExample();
        TbOutdoorSignUserExample.Criteria signUserCriteria = signUserExample.createCriteria();
        signUserCriteria.andOutdoorOrderIdEqualTo(vOrderActivityExt.getId());
        List<TbOutdoorSignUser> tbOutdoorSignUsers = signUserMapper.selectByExample(signUserExample);

        vOrderActivityExt.setUser(tbOutdoorSignUsers);
        TbOutDoorActivityExt tbOutDoorActivityExt = new TbOutDoorActivityExt();
        TbOutdoorActivity activity = tbOutdoorActivityMapper.selectByPrimaryKey(vOrderActivityExt.getActivityId());
        BeanUtils.copyProperties(activity,tbOutDoorActivityExt);

        TbOutdoorDiscountExample discountExample = new TbOutdoorDiscountExample();
        TbOutdoorDiscountExample.Criteria disCountCriteria = discountExample.createCriteria();
        disCountCriteria.andActivityIdEqualTo(vOrderActivityExt.getId());
        List<TbOutdoorDiscount> discounts = tbOutdoorDiscountMapper.selectByExample(discountExample);
        if(!CollectionUtils.isEmpty(discounts)){
            vOrderActivityExt.setDiscuntId(discounts.get(0).getId());
            vOrderActivityExt.setDiscuntName(discounts.get(0).getName());
        }
        if(vOrderActivityExt.getDeadTime().after(new Date())){
            vOrderActivityExt.setIsFinish(1);
        }
        if(tbOutDoorActivityExt.getType() == 0){
            TbOutdoorProductExample productExample = new TbOutdoorProductExample();
            TbOutdoorProductExample.Criteria productCriteria = productExample.createCriteria();
            productCriteria.andActivityIdEqualTo(tbOutDoorActivityExt.getId());
            List<TbOutdoorProduct> products = tbOutdoorProductMapper.selectByExample(productExample);
            tbOutDoorActivityExt.setProductList(products);

        }else{
            TbOutdoorThemeExample themeExample = new TbOutdoorThemeExample();
            TbOutdoorThemeExample.Criteria themeCriteria = themeExample.createCriteria();
            themeCriteria.andActivityIdEqualTo(tbOutDoorActivityExt.getId());
            List<TbOutdoorTheme> themes = themeMapper.selectByExample(themeExample);
            List<Integer> id = new ArrayList<>();
            for(TbOutdoorTheme theme : themes){
                id.add(theme.getProductId());
            }
            TbOutdoorProductExample productExample = new TbOutdoorProductExample();
            TbOutdoorProductExample.Criteria productCriteria = productExample.createCriteria();
            productCriteria.andIdIn(id);
            List<TbOutdoorProduct> products = tbOutdoorProductMapper.selectByExample(productExample);
            List<Integer> activityIds = new ArrayList<>();
            if(!CollectionUtils.isEmpty(products)){
                for(TbOutdoorProduct product : products){
                    activityIds.add(product.getActivityId());
                }
            }
            TbOutdoorActivityExample activityExample = new TbOutdoorActivityExample();
            TbOutdoorActivityExample.Criteria activityCriteria = activityExample.createCriteria();
            activityCriteria.andIdIn(activityIds);
            List<TbOutdoorActivity> activities = tbOutdoorActivityMapper.selectByExample(activityExample);
            tbOutDoorActivityExt.setActivityList(activities);
        }
        respOrderDetails.setActivityMsg(tbOutDoorActivityExt);
        return APIResponse.returnSuccess(respOrderDetails);
    }

    @Override
    public APIResponse clearCache() {
        RedisKey rk = new RedisKey(RedisConstants.OUTDOOR_FAMILY_KEY, OutdoorConstants.INDEX_CACHE_KEY);
        redisTemplate.setWithExpireTime(rk, null,cache_expire_time);
        return APIResponse.returnSuccess("清除成功");
    }
}
